package com.metasoft.flex.dto {
	
	import com.metasoft.flex.MetasoftULC;
	import com.metasoft.flex.util.MetasoftHelper;
	
	public class QueryInfo {
		
		public var title:String;
		public var synopsis:String;
		public var genres:Array;
		public var crid:String;
		public var creditNamess:Array;
		
		public function set setTitle(title:String):void {
			this.title = new String();
			this.title = title;
		}

		public function set setSynopsis(synopsis:String):void {
			this.synopsis = new String();
			this.synopsis = synopsis;
		}
		
		public function set setGenress(genres:Array):void {
			this.genres = new Array();
			this.genres = genres;
		}
		
		public function set setCrid(crid:String):void {
			this.crid = new String(); 
			this.crid = crid;
		}
		
		public function set setCreditNamess(creditNamess:Array):void {
			this.creditNamess = new Array();
			this.creditNamess = creditNamess;
		}
		
		public function toGetDataXMLString():String {
			var getDataStr:String = new String();
			
			getDataStr = getDataStr.concat(getDataHeader());
			getDataStr = getDataStr.concat("<tns:ResourceID/>");
			getDataStr = getDataStr.concat("<tns:QueryConstraints>");
			getDataStr = getDataStr.concat("<tns:PredicateBag type =\"OR\">");
			
			if (title != null) {
            	getDataStr = getDataStr.concat(createBinaryPredicate(MetasoftULC.Field_Title, title));    
   			}

			if (synopsis != null) {
				getDataStr = getDataStr.concat(createBinaryPredicate(MetasoftULC.Field_Synopsis, synopsis));
			}
			
			if (crid != null) {
				getDataStr = getDataStr.concat(createBinaryPredicate(MetasoftULC.Field_CRID, crid));
			}
			
			if (genres != null) {
				for (var genre:String in genres) {
					if(MetasoftULC.queryAll) {
						var genreArray:Array = MetasoftHelper.getCategoryChildren(genre);
						for (var i:int = 0; i < genreArray.length; i++) {
							getDataStr = getDataStr.concat(createBinaryPredicate(MetasoftULC.Field_Genre, genreArray[i]));
						}
					} else {
						getDataStr = getDataStr.concat(createBinaryPredicate(MetasoftULC.Field_Genre, genre));
					}
				}	
			}
			
			if (creditNamess != null) {
				for (var creditName:String in creditNamess) {
					getDataStr = getDataStr.concat(createBinaryPredicate(MetasoftULC.Field_CreditName, creditNamess[creditName]));
				}
			}
			
   			getDataStr = getDataStr.concat("</tns:PredicateBag>");
   			getDataStr = getDataStr.concat("</tns:QueryConstraints>");
			getDataStr = getDataStr.concat(getPersonalInformation());
			getDataStr = getDataStr.concat("<tns:RequestedTables><tns:Table type=\"ProgramInformationTable\"/></tns:RequestedTables>");
			
			getDataStr = getDataStr.concat(getDataFooter());
			return getDataStr;
		}
		
		private function getPersonalInformation():String {
			
			var prefStr:String = new String();
			prefStr = prefStr.concat("<tns:PersonalInformation>");
			prefStr = prefStr.concat("<tva:UserPreferences>");
			prefStr = prefStr.concat("<mpeg7:FilteringAndSearchPreferences>");
			prefStr = prefStr.concat("<mpeg7:ClassificationPreferences>");
			
			if (genres != null) {
				for (var genreName:String in genres) {
					prefStr = prefStr.concat("<mpeg7:Genre preferenceValue=\"").concat(genres[genreName]).concat("\">");
					prefStr = prefStr.concat(getNameNode(genreName));
					prefStr = prefStr.concat("</mpeg7:Genre>");
				}
			}
			prefStr = prefStr.concat("</mpeg7:ClassificationPreferences>");
			prefStr = prefStr.concat("</mpeg7:FilteringAndSearchPreferences>");
			prefStr = prefStr.concat("</tva:UserPreferences>");
   			prefStr = prefStr.concat("</tns:PersonalInformation>");
   			
   			return prefStr;
		}
		
		private function getNameNode(name:String):String {
			return "<mpeg7:Name>" + name + "</mpeg7:Name>";
		}
		
		private function getDataHeader():String {
			var xmlStr:String = new String();
				xmlStr = xmlStr.concat("<?xml version=\"1.0\" encoding=\"UTF-8\"?>", 
				"<tns:get_Data xmlns:tns=\"urn:tva:transport:2005\" " ,
				"xmlns:p3p=\"http://www.w3.org/2002/01/P3Pv1\" " , 
				"xmlns:cr=\"urn:tva:ContentReferencing:2005\" " , 
				"xmlns:rar=\"urn:tva:ResolvingAuthority:2005\" " , 
				"xmlns:tva=\"urn:tva:metadata:2005\" " , 
				"xmlns:tva2=\"urn:tva:metadata:extended:2005\" " , 
				"xmlns:n1=\"urn:tva:metadata:extension:2005\" " , 
				"xmlns:int=\"urn:tva:metadata:interstitial:2005\" " , 
				"xmlns:mpeg21=\"urn:tva:mpeg21:2005\" " , 
				"xmlns:mpeg7=\"urn:tva:mpeg7:2005\" " , 
				"xmlns:rmpi=\"urn:tva:rmpi:2005\" " , 
				"xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " , 
				"personalInfoUse=\"true\" maxPrograms=\"0\">");
			return xmlStr;	
		}	
		
		private function getDataFooter():String {
			return "</tns:get_Data>";	
		}
		
		private function createBinaryPredicate(fieldId:String, fieldValue:String):String {
			
			var bpStr:String = new String();
			bpStr = bpStr.concat("<tns:BinaryPredicate fieldID=\"");
			bpStr = bpStr.concat(fieldId);
			bpStr = bpStr.concat("\" fieldValue=\""); 
			bpStr = bpStr.concat(fieldValue);
			bpStr = bpStr.concat("\" test=\"equals\"/>");
			
			return bpStr;
		}
	}
}